﻿using Microsoft.Extensions.DependencyInjection;
using MongoDB.Driver;
using Testudo.Core;

namespace Testudo.Mongo;

public static class ServicesExtensions
{
    public static void RegisterMongo(this IServiceCollection services)
    {
        services.AddScoped(typeof(IRepository<>), typeof(MongoRepository<>));
        services.AddScoped(typeof(IRepository<,,,>), typeof(MongoRepository<,,,>));

        services.AddScoped(typeof(ITenantRepository<>), typeof(TenantMongoRepository<>));

        services.AddScoped<IMongoClient>(options =>
        {
            var mongoConfig = options.GetService<MongoConfig>();
            return new MongoClient(mongoConfig.Connection);
        });

        services.AddScoped(options =>
        {
            var client = options.GetService<IMongoClient>();
            var mongoConfig = options.GetService<MongoConfig>();
            return client.GetDatabase(mongoConfig.Database);
        });
    }
}